﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Pitaya_IDE
{
    //横向标题栏类
    public class TitlePanel : Panel
    {
        //public Inode inode;
        public int cnt;  //指示当前这个标签位置
        public bool Selected = false;//当前文件是否呗选中
        private int height;
        string suffix;
        public bool Inodedel = false;//当前文件是否被删除

        FileTitleBar fileTitleBar = null;//上级类(管理)
        public SubPanel subPanel = null;

        Label labelPic, labelText, labelClose, labelLine;
        Label[] lineLabel = new Label[4];

        //public TitlePanel(Inode _inode,int _height, FileTitleBar _fileTitleBar)
        //{
        //    inode = _inode;
        //    height = _height;
        //    fileTitleBar = _fileTitleBar;
        //    subPanel = fileTitleBar.subPanel;

        //    //inode.Observer(this);
        //    inode.ObserverFile(subPanel);//add//添加文件变动监视同时获取文件内容

        //    string[] sArray = inode.name.Split(new char[1] { '.' });

        //    if (sArray.Length > 1)
        //    {
        //        suffix = sArray[sArray.Length - 1];
        //    }
        //    else
        //    {
        //        suffix = null;
        //    }


        //    this.Name = "titlePanel";
        //    this.BackColor = Color.FromArgb(1, 4, 9);
        //    this.Font = new Font("等线", 13);
        //    this.Cursor = Cursors.Hand;


        //    labelPic = new Label
        //    {
        //        Name = "labelPic",
        //        Size = new Size(20, 20),
        //        Location = new Point(24,20),
        //        BackColor = Color.FromArgb(1, 4, 9),
        //        Font = new Font("等线", 13),
        //        Cursor = Cursors.Hand,
        //    };

        //    SetFileImage(1);

        //    labelText = new Label()
        //    {
        //        Name = "labelText",
        //        Text = inode.name,
        //        AutoSize = true,
        //        Location = new Point(52, 20),
        //        BackColor = Color.FromArgb(1, 4, 9),
        //        ForeColor = Color.FromArgb(201, 209, 217),
        //        Font = new Font("等线", 18),
        //        Cursor = Cursors.Hand,
        //    };

        //    labelClose = new Label()
        //    {
        //        Name = "labelClose",
        //        Size = new Size(34, 34),
        //        BackColor = Color.FromArgb(1, 4, 9),
        //        Cursor = Cursors.Hand,
        //    };

        //    labelClose.Image = AllImage.CloseMode1;

        //    labelLine = new Label()
        //    {
        //        Name = "labelClose",
        //        Location = new Point(0,0),
        //        Size = new Size(210, 2),
        //        BackColor = Color.FromArgb(247, 129, 102),
        //        Cursor = Cursors.Hand,
        //    };

        //    for (int i = 0; i < 4; i++)
        //    {
        //        lineLabel[i] = new Label()
        //        {
        //            Name = "lineLabel" + i,
        //            BackColor = Color.FromArgb(48, 54, 61),
        //            Cursor = Cursors.Default,
        //        };
        //        this.Controls.Add(lineLabel[i]);
        //    }

        //    lineLabel[0].Visible = false;
        //    lineLabel[1].Visible = false;

        //    this.Controls.Add(labelPic);
        //    this.Controls.Add(labelText);
        //    this.Controls.Add(labelClose);
        //    this.Controls.Add(labelLine);


        //    labelText.MouseDown += new MouseEventHandler(File_MouseDown);
        //    labelText.MouseUp += new MouseEventHandler(File_MouseUP);
        //    labelText.MouseMove += new MouseEventHandler(File_MouseMove);
        //    labelText.MouseEnter += new EventHandler(File_MouseEnter);
        //    labelText.MouseLeave += new EventHandler(File_MouseLeave);

        //    labelPic.MouseDown += new MouseEventHandler(File_MouseDown);
        //    labelPic.MouseUp += new MouseEventHandler(File_MouseUP);
        //    labelPic.MouseMove += new MouseEventHandler(File_MouseMove);
        //    labelPic.MouseEnter += new EventHandler(File_MouseEnter);
        //    labelPic.MouseLeave += new EventHandler(File_MouseLeave);


        //    labelClose.MouseDown += new MouseEventHandler(Close_MouseDown);
        //    labelClose.MouseUp += new MouseEventHandler(Close_MouseUP);
        //    labelClose.MouseEnter += new EventHandler(Close_MouseEnter);
        //    labelClose.MouseLeave += new EventHandler(Close_MouseLeave);

        //    this.MouseDown += new MouseEventHandler(File_MouseDown);
        //    this.MouseUp += new MouseEventHandler(File_MouseUP);
        //    this.MouseMove += new MouseEventHandler(File_MouseMove);
        //    this.MouseEnter += new EventHandler(File_MouseEnter);
        //    this.MouseLeave += new EventHandler(File_MouseLeave);

        //}

        public void SetCnt(int _cnt,int Left_dat)
        {
            cnt = _cnt;

            this.Size = new Size(210, height);
            labelClose.Location = new Point(labelText.Left + labelText.Width, 14);
            
            if (labelClose.Left < 165) labelClose.Left = 165;

            this.Width = labelClose.Left + labelClose.Width + 8;
            labelLine.Width = this.Width;

            lineLabel[2].Location = new Point(this.Width - 1, 0);
            lineLabel[2].Size = new Size(1, this.Height);

            lineLabel[3].Location = new Point(0, this.Height - 1);
            lineLabel[3].Size = new Size(this.Width, 1);

            if (cnt == 0)
            {
                this.Location = new Point(Left_dat + 0, 0);
            }
            else//根据前面一个标签计算
            {
                TitlePanel tmp = fileTitleBar.titlePanelList[cnt - 1];
                this.Location = new Point(Left_dat + tmp.Left + tmp.Width, 0); //根据前面一个Panel计算坐标
            }

            if (Selected)
            {
                labelLine.Visible = true;
                lineLabel[3].Visible = false;
            }
            else
            { 
                labelLine.Visible = false;
                lineLabel[3].Visible = true;
            }
        }

        private bool inode_Sel = false;
        public void SetSelec(bool _Selected)
        {
            Color set_cloor;

            if (_Selected)
            {
                labelLine.Visible = true;
                lineLabel[3].Visible = false;

                set_cloor = Color.FromArgb(13, 17, 23);
                SetFileImage(13);
                labelClose.Image = AllImage.CloseMode4;

                if (!Selected)//根据上一次的值
                {
                    //if (inode != null && !Inodedel)
                    //{
                    //    inode.Select = true;
                    //    inode_Sel = true;
                    //}
                    ///*修改左边起始坐标*/
                }
            }
            else
            {
                labelLine.Visible = false;
                lineLabel[3].Visible = true;

                set_cloor = Color.FromArgb(1, 4, 9);
                SetFileImage(1);
                labelClose.Image = AllImage.CloseMode1;

                if (Selected)//根据上一次的值
                {
                    //if (inode != null && !Inodedel)
                    //{ 
                    //    inode.Select = false;
                    //    inode_Sel = false;
                    //}
                }
            }

            Selected = _Selected;

            this.BackColor = set_cloor;
            labelText.BackColor = set_cloor;
        }

        private bool focus1 = false;  //表示当前是否拥有焦点  
        public bool Focus1
        {
            get => focus1;
            set
            {
                focus1 = value;
                //if (value)
                //{
                //    labelLine.BackColor = Color.FromArgb(247, 129, 102);
                //    labelText.ForeColor = Color.FromArgb(201, 209, 217);
                //    if (inode_Sel && !inode.Select)
                //    {
                //        inode.Select = true;
                //    }
                //}
                //else
                //{
                //    labelLine.BackColor = Color.FromArgb(48, 54, 61);
                //    labelText.ForeColor = Color.FromArgb(100, 104, 108);
                //    if (inode_Sel && inode.Select)
                //    {
                //        inode.Select = false;
                //    }
                //}
            }
        }

        public void SetInodedel()//设置文件被删除
        {
            labelText.Text = labelText.Text + "已删除";
            Inodedel = true;
        }

        private bool Mouseflag = false;
        private void File_MouseUP(object sender, EventArgs e)
        {
            if (Mouseflag)
            {
                if (!Selected) fileTitleBar.SetSelected(this);
            }
            Mouseflag = false;
        }
        private void File_MouseDown(object sender, EventArgs e)
        {
            fileTitleBar.subPanel.Focus1 = true;  //设置this的上级面板拥有焦点
            Mouseflag = true;
        }
        private void File_MouseMove(object sender, MouseEventArgs e)
        {
            if (Mouseflag)
            {
                
            }
        }
        private void File_MouseEnter(object sender, EventArgs e)
        {
            Color set_cloor = Color.FromArgb(13, 17, 23);

            this.BackColor = set_cloor;
            labelText.BackColor = set_cloor;
            SetFileImage(13);

            if (Selected)
            {
                labelClose.Image = AllImage.CloseMode4;
            }
            else
            {
                labelClose.Image = AllImage.CloseMode5;
            }
        }
        private void File_MouseLeave(object sender, EventArgs e)
        {
            Color set_cloor;

            Point panelPoint = this.PointToClient(Control.MousePosition); //鼠标相对于pane左上角的坐标

            if (panelPoint.X > 5 && panelPoint.X < this.Width - 5 &&
                panelPoint.Y > 5 && panelPoint.Y < this.Height - 5)
            {
                return;
            }

            if (Selected)
            {
                set_cloor = Color.FromArgb(13, 17, 23);
                SetFileImage(13);
                labelClose.Image = AllImage.CloseMode4;
            }
            else
            {
                set_cloor = Color.FromArgb(1, 4, 9);
                SetFileImage(1);
                labelClose.Image = AllImage.CloseMode1;
            }

            this.BackColor = set_cloor;
            labelText.BackColor = set_cloor;
        }

        private bool MouseCloseflag = false;
        private void Close_MouseUP(object sender, EventArgs e)
        {
            if (MouseCloseflag)
            {
                Panel tmp =  (Panel)(this.Parent);
                tmp.Controls.Remove(this); //从父容器中删除本控件

                //if (fileTitleBar != null)
                //{
                //    fileTitleBar.RmTitlePanel(inode);
                //    fileTitleBar.RefreshLabel();
                //}

                //if (inode != null && !Inodedel)
                //{
                //    inode.Select = false;    //减少选中

                //    inode.DeleteMonitor(subPanel);  //减少文件变动监视
                //    inode = null;
                //}
            }
            MouseCloseflag = false;
        }
        private void Close_MouseDown(object sender, EventArgs e)=>MouseCloseflag = true;
        private void Close_MouseEnter(object sender, EventArgs e)
        {
            Color set_cloor = Color.FromArgb(13, 17, 23);

            this.BackColor = set_cloor;
            labelText.BackColor = set_cloor;
            SetFileImage(13);

            if (Selected)
            {
                labelClose.Image = AllImage.CloseMode2;
            }
            else
            {
                labelClose.Image = AllImage.CloseMode3;
            }
        }
        private void Close_MouseLeave(object sender, EventArgs e)
        {
            
            Point panelPoint = this.PointToClient(Control.MousePosition); //鼠标相对于pane左上角的坐标
            if (!(panelPoint.X > 0 && panelPoint.X < this.Width -3 &&
                panelPoint.Y > 3 && panelPoint.Y < this.Height - 3))
            {
                Color set_cloor;
                if (Selected)
                {
                    set_cloor = Color.FromArgb(13, 17, 23);
                    SetFileImage(13);
                    labelClose.Image = AllImage.CloseMode4;
                }
                else
                {
                    set_cloor = Color.FromArgb(1, 4, 9);
                    SetFileImage(1);
                    labelClose.Image = AllImage.CloseMode1;
                }

                this.BackColor = set_cloor;
                labelText.BackColor = set_cloor;
            }
            else
            {
                if (Selected)
                {
                    labelClose.Image = AllImage.CloseMode4;
                }
                else
                {
                    labelClose.Image = AllImage.CloseMode5;
                }
            }
        }


        //public Inode TitleInode
        //{
        //    get{
        //        return inode;
        //    }
        //}


        private void SetFileImage(int bright)
        {
            if (suffix != null)
            {
                if (suffix.Equals("py") || suffix.Equals("PY"))
                {
                    labelPic.Image = AllImage.GetImage("py", bright);
                }
                else if (suffix.Equals("c") || suffix.Equals("C") || suffix.Equals("h") || suffix.Equals("H"))
                {
                    labelPic.Image = AllImage.GetImage("c", bright);
                }
                else if (suffix.Equals("sh") || suffix.Equals("SH"))
                {
                    labelPic.Image = AllImage.GetImage("sh", bright);
                }
                else //无法识别的后缀
                {
                    labelPic.Image = AllImage.GetImage("default", bright);
                }
            }
            else //无后缀
            {
                labelPic.Image = AllImage.GetImage("default", bright);
            }
            
        }
    }
}